import java.io.*;
import java.util.Random;
import org.kwis.msp.lcdui.Card;
import org.kwis.msp.lcdui.Display;
import org.kwis.msp.lcdui.Image;
import org.kwis.msp.lcdui.Graphics;
import org.kwis.msp.lcdui.EventQueue;

class TetrisCard extends Card implements Runnable{
    private Image gameoverImg;
    private Image scoreNumImg[];
    private Image scorePanelImg;
    private Image nextPanelImg;
	private Block block;
	private static int GAMESPEED = 1;

    public TetrisCard() {
        try {
            gameoverImg = Image.createImage("/gmov.png");       // 이미지 초기화
            scorePanelImg = Image.createImage("/score.png");
           nextPanelImg = Image.createImage("/next.png");
            scoreNumImg = new Image[10];
            for(int i = 0; i < 10; i++)
                scoreNumImg[i] = Image.createImage("/" + i + ".png");
        } catch(IOException e) {
            System.out.println("LOAD Error : " + e);
        }
		block=getBlock();
    }
    
	public Block getBlock() {
        Random random = new Random();
		int blockType = ((int) (Math.abs(random.nextInt()) % 7)) + 1;
		return new Block(blockType);
	}

	private void showScorePanel(Graphics g) {
        g.drawImage(scorePanelImg, 65, 50, g.LEFT | g.TOP);
    }

    private void showNextPanel(Graphics g) {
        g.drawImage(nextPanelImg, 65, 10, g.LEFT | g.TOP);
    }
    
    protected  void paint(Graphics g) {
        g.setColor(0, 0, 0);   // 배경 색상 지정(검정색)
        g.fillRect(0, 0, getWidth(), getHeight());   // 배경 색칠
        g.setColor(255, 255, 0);   // 오른쪽 경계선 그리기
        g.drawLine(62, 10, 62, getHeight());
        showScorePanel(g);    // 점수판 그리기
        showNextPanel(g);     // next판 그리기
		block.drawBlock(g);
    }

	public void run() {
        try {
            while(true) {
                gameOn();
                Thread.sleep((int) (1000 / GAMESPEED));
            }
        }catch(Exception e) {  }
    }

	private void gameOn() {
        block.move(0, 1);
		repaint();
    }
}


